package com.gelsanalyzer.ui.swing;

import java.awt.event.*;

import javax.swing.*;

/**
 * A WindowListener class that enables us to keep a consistent event model
 * across our CourseMgmt related Use Cases.
 * 
 * @see WindowAdapter
 * @author Jared Olhoft
 * @version 1.0, 2/20/2008
 * @since 1.0
 */
class CourseMgmtWindowListener extends WindowAdapter {
    private JFrame window;

    /**
     * The constructor takes the JFrame window this WindowAdapter is being
     * applied to as a parameter.
     * 
     * @precondition The JFrame passed in should have
     *               <code>CourseMgmtMenuBar</code> set as its
     *               <code>JMenuBar</code> private member.
     * @param window
     */
    public CourseMgmtWindowListener(JFrame window) {
        this.window = window;
    }

    /**
     * Allows us to handle the closing of the window in a consistent manner.
     * Specifically, this allows us to get a reference to the JMenuItem that
     * extends the <code>Command</code> model that will allow us to fire one
     * command for many purposes.
     */
    public void windowClosing(WindowEvent e) {
        CourseMgmtMenuBar menubar = null;
        JMenuItem menuItem = null;

        if ( window.getJMenuBar() instanceof CourseMgmtMenuBar ) {
            menubar = (CourseMgmtMenuBar) window.getJMenuBar();
            menuItem = menubar.getFileExitMenuItem();
        }
        if ( menuItem != null ) {
            menuItem.doClick();
        } else {
            System.exit(0);
        }
    }
}
